import 'package:animations/animations.dart';
import 'package:flutter/material.dart';
import 'package:flutter_study/provider/topics_page_model.dart';
import 'package:flutter_study/widget/loading_container.dart';
import 'package:flutter_study/widget/provider_widget.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

import 'topic_detail_page.dart';
import 'topic_widget_item.dart';

class TopicsPage extends StatefulWidget {
  @override
  _TopicsPageState createState() => _TopicsPageState();
}

class _TopicsPageState extends State<TopicsPage>
    with AutomaticKeepAliveClientMixin {
  @override
  Widget build(BuildContext context) {
    super.build(context);
    return ProviderWidget<TopicsPageModel>(
        model: TopicsPageModel(),
        onModelInit: (model) {
          model.refresh();
        },
        builder: (context, model, child) {
          return LoadingContainer(
            loading: model.loading,
            error: model.error,
            retry: model.retry,
            child: SmartRefresher(
              controller: model.refreshController,
              onRefresh: model.refresh,
              onLoading: model.loadMore,
              enablePullUp: true,
              child: ListView.builder(
                itemCount: model.itemList.length,
                itemBuilder: (context, index) {
                  return OpenContainer(
                    closedBuilder: (context, action) {
                      return TopicWidgetItem(
                        itemModel: model.itemList[index],
                      );
                    },
                    openBuilder: (context, action) {
                      return TopicDetailPage(
                        detailId: model.itemList[index].data.id,
                      );
                    },
                  );
                },
              ),
            ),
          );
        });
  }

  @override
  bool get wantKeepAlive => true;
}
